package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.*;
import net.lab1024.sa.admin.module.business.erp.domain.entity.*;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.MmordAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.MmordQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.MmordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.MmordVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 工单 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 08:57:48
 * @Copyright AKITA
 */

@Service
public class MmordService {

    @Resource
    private MmordDao mmordDao;
    @Resource
    private MmordaDao mmordaDao;
    @Resource
    private MmordbDao mmordbDao;
    @Resource
    private CalendarsaDao calendarsaDao;
    @Resource
    private AccountbDao accountbDao;
    @Resource
    private AccountcDao accountcDao;
    @Resource
    private EmployeeDao employeeDao;
    @Autowired
    private AccountDao accountDao;

    /**
     * 分页查询
     */
    public PageResult<MmordVO> queryPage(MmordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<MmordVO> list = mmordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<Long> add(MmordAddForm addForm) {
        MmordEntity mmordEntity = SmartBeanUtil.copy(addForm, MmordEntity.class);
        mmordDao.insert(mmordEntity);
        return ResponseDTO.ok(mmordEntity.getDocEntry());
    }

    /**
     * 更新
     */

    @Transactional
    public ResponseDTO<String> update(MmordUpdateForm updateForm) {
        MmordEntity mmordEntity = SmartBeanUtil.copy(updateForm, MmordEntity.class);
        if (mmordDao.updateById(mmordEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        mmordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        MmordEntity mmordEntity = mmordDao.selectById(docEntry);
        AccountEntity accountEntity = accountDao.selectOne(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, mmordEntity.getAbsId()));
        if (accountEntity != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST,"存在未删除的汇总单据！，请先删除工资单");
        }
        mmordDao.deleteById(docEntry);
        mmordaDao.deleteById(docEntry);
        mmordbDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<MmordEntity> info(Long docEntry) {
        MmordEntity mmordEntity =  mmordDao.info(docEntry);
        mmordEntity.setLista(mmordaDao.selectList(new LambdaQueryWrapper<MmordaEntity>().eq(true, MmordaEntity::getDocEntry, docEntry)));
        mmordEntity.setListb(mmordbDao.selectList(new LambdaQueryWrapper<MmordbEntity>().eq(true, MmordbEntity::getDocEntry, docEntry)));
        mmordEntity.setListc(accountbDao.selectList(new LambdaQueryWrapper<AccountbEntity>().eq(true, AccountbEntity::getBaseEntry, docEntry)));
        mmordEntity.setListd(accountcDao.selectList(new LambdaQueryWrapper<AccountcEntity>().eq(true, AccountcEntity::getMmordEntry, docEntry)));
        return ResponseDTO.ok(mmordEntity);

    }

    public ResponseDTO<MmordEntity> myInfo(Long docEntry, Long userId) {
        MmordEntity mmordEntity = mmordDao.selectById(docEntry);
        mmordEntity.setListb(mmordbDao.selectList(new LambdaQueryWrapper<MmordbEntity>().eq(MmordbEntity::getDocEntry, docEntry).eq(MmordbEntity::getUserId, userId)));
        return ResponseDTO.ok(mmordEntity);
    }

    public ResponseDTO<MmordEntity> batchInsert(Long docEntry) {
        MmordEntity mmordEntity = mmordDao.selectById(docEntry);
        List<CalendarsaEntity> calendarsaEntities = calendarsaDao.selectList(new LambdaQueryWrapper<CalendarsaEntity>().eq(CalendarsaEntity::getAbsId, mmordEntity.getAbsId()));
        List<EmployeeEntity> employeeEntities = employeeDao.selectList(null);
        MmordbEntity mmordbEntity = new MmordbEntity();
        mmordbEntity.setDocEntry(mmordEntity.getDocEntry());
        int lineNum = 1;
        for (EmployeeEntity employeeEntity : employeeEntities) {
            if (employeeEntity.getDepartmentId() == 30){
                for (CalendarsaEntity calendarsaEntity : calendarsaEntities) {
                    mmordbEntity.setLineNum(lineNum++);
                    mmordbEntity.setUserId(employeeEntity.getEmployeeId());
                    mmordbEntity.setUserName(employeeEntity.getLoginName());
                    mmordbEntity.setNowDate(calendarsaEntity.getNowDate());
                    mmordbEntity.setAbsId(calendarsaEntity.getAbsId());
                    mmordbEntity.setWorkTime(BigDecimal.ZERO);
                    mmordbEntity.setCommon(BigDecimal.ZERO);
                    mmordbEntity.setOverTime(BigDecimal.ZERO);
                    mmordbEntity.setHoliday(BigDecimal.ZERO);
                    mmordbEntity.setDel("a");
                    mmordbEntity.setTime9(true);
                    mmordbEntity.setTime10(true);
                    mmordbEntity.setTime11(true);
                    mmordbEntity.setTime12(true);

                    mmordbEntity.setTime15(true);
                    mmordbEntity.setTime16(true);
                    mmordbEntity.setTime17(true);
                    mmordbEntity.setTime18(true);


                    if (calendarsaEntity.getWorkDay()){
                        mmordbEntity.setWorkTime(BigDecimal.valueOf(8));
                    } else {
                        mmordbEntity.setHoliday(BigDecimal.valueOf(8));
                    }
                    mmordbEntity.setWeek(calendarsaEntity.getWeek());
                    mmordbEntity.setWorkDay(calendarsaEntity.getWorkDay());
                    mmordbDao.insert(mmordbEntity);
                }
            }
        }
        return ResponseDTO.ok();
    }
}
